<template>
  <a-modal
    class="width-mad-style"
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    @ok="handleCancel"
    cancelText="关闭">
    <a-row :gutter="20">
      <a-form :form="form">
          <a-form-item style="">
            <a-button type="primary" @click="DownLoadClick" style="margin-left: 10px">恢复</a-button>
<!--            <a-button type="primary" @click="DownLoadClick(docName,recordId)" style="margin-left: 15px">文档打印</a-button>-->
          </a-form-item>
      <a-col :span="24">
        <a-spin :spinning="confirmLoading">

            <a-row :gutter="26">
              <a-col class="gutter-row" :span="26">
                <div class="gutter-box">
                  <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <j-editor v-decorator="['editor']"  :readonly="true"/>
                  </a-form-item>
                </div>
              </a-col>
            </a-row>

        </a-spin>
      </a-col>
      </a-form>
    </a-row>

  </a-modal>
</template>

<script>
  import JCategorySelect from '@/components/jeecg/JCategorySelect'
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import {getAction} from '@/api/manage'
  import {postAction,handleExportDoc} from '@/api/manage'

  export default {
    name: "EditorOpenModal",
    components: {
      JDate,
      JDictSelectTag,
      JEditor,
      JCategorySelect
    },
    data() {
      return {
        id:"",
        docName:'',
        recordId:'',
        formLayout: 'horizontal',
        // form: this.$form.createForm(this, { name: 'coordinated' }),
        temp: "",
        form: this.$form.createForm(this),
        title: "操作",
        // width: 100%,
        visible: false,
        model: '',
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        url: {
          list: "/doc/editor/list",
          add: "/doc/doc/add",
          addEditor: "/doc/doc/addEditor",
          edit: "/doc/doc/edit",
          queryById: "/doc/doc/queryById",
          queryId: "/doc/doc/queryId",
          recover: "/doc/doc/recover",
          queryHistoryById: "/doc/docHistoryCopy/queryById",
        }
      }
    },
    created() {
    },
    methods: {
      DownLoadClick() {
        const that=this
        getAction(this.url.queryHistoryById,{id:this.id.toString()}).then((res) => {
          if (res.success) {
            postAction(this.url.recover,res.result).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            })
            this.loading = false
          }
        })
        this.handleCancel()
      },
      handleSubmit(e) {
        e.preventDefault();
        this.form.validateFields((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values);
          }
        });
      },
      handleSelectChange(value) {
        console.log(value);
        this.form.setFieldsValue({
          note: `Hi, ${value === 'male' ? 'man' : 'lady'}!`,
        });
      },
      //文档内容管理创建模块
      open(operation) {
        if (operation == 1) {
          getAction(this.url.list, 'get').then((res) => {
            //alert(JSON.stringify(res))
            if (res.success) {
              this.model = res.result.records[0].editor
              this.loading = false
            }
          })
          this.form.resetFields();
          this.visible = true;
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, 'editor'))
            //alert(JSON.stringify(this.model))
          })
        } else if (operation == 2) {
          this.form.resetFields();
          this.visible = true;
        }
      },
      //文档内容管理查看按钮
      edit(key) {
        getAction(this.url.queryById,{id:key.toString()}).then((res) => {
          if (res.success) {
            this.model = res.result.editor
            this.recordId = res.result.id
            this.docName = res.result.docName
            this.$nextTick(() => {
              this.form.setFieldsValue({editor:this.model})
            })
            this.loading = false
          }
        })
        //this.form.resetFields();
        this.visible = true;
      },
      editOpen(key) {
        this.id=key
        getAction(this.url.queryById,{id:key.toString()}).then((res) => {
          if (res.success) {
            this.model = res.result.editor
            this.recordId = res.result.id
            this.docName = res.result.docName
            this.$nextTick(() => {
              this.form.setFieldsValue({editor:this.model})
            })
            this.loading = false
          }
        })
        //this.form.resetFields();
        this.visible = true;
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      add() {
        const that = this;
        var formData = {
          editor: this.model
        }
        postAction(this.url.addEditor, formData, 'post').then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.$emit('ok');
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
          that.close();
        })
      },
      //@ok="handleOk"
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row,'editor'))
      },


    }
  }
</script>